package com.example.neusoftfood_813uk.model;

import com.example.neusoftfood_813uk.bean.Loginbean;
import com.example.neusoftfood_813uk.common.Common;
import com.example.neusoftfood_813uk.listener.LoginListener;
import com.example.neusoftfood_813uk.service.Loginservice;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel {
    private Loginservice service;
    private Retrofit retrofit;

    public LoginModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(Loginservice.class);
    }
    public void userLogin(String username, String userpass, final LoginListener listener)  {
        Call call=service.login(username, userpass);
        Callback<Loginbean> callback = new Callback<Loginbean>() {
            @Override
            public void onResponse(Call<Loginbean> call, Response<Loginbean> response) {
                listener.onSuccess(response.body(),1);
            }

            @Override
            public void onFailure(Call<Loginbean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}

